package com.fahai.web.controller;

import com.fahai.utils.json.JsonResult;
import com.fahai.web.service.RoleMenuService;
import com.fahai.web.vo.SysGroupVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by sky on 17/5/11.
 * 权限组修改控制器
 */
@Controller
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequestMapping("/sysuser")
public class SysMenuController extends BaseController {
    static Logger logger = LoggerFactory.getLogger(SysMenuController.class);

    @Autowired
    RoleMenuService roleMenuService;


    /**
     * 返回角色组权限列表数据
     *
     * @param model
     * @return
     */
    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/enterSysMenu", method = RequestMethod.GET)
    public String enterSysGroupManage(Model model) {
        List<SysGroupVo> sysGroupVoList = roleMenuService.findMenuList();
        model.addAttribute("sysGroupVoList", sysGroupVoList);
        return "sysuser_menu_list";
    }

    /**
     * 修改权限组
     * @param sysGroupVo
     * @return
     */
    @RequestMapping(value = "/updateSysMenu",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult updateSysMenuAuth(@RequestParam(required = false) SysGroupVo sysGroupVo ){
        roleMenuService.updateSysMenu(sysGroupVo);

        return ok("修改成功");
    }

    /**
     * 添加权限组
     * @param sysGroupVo
     * @return
     */
    @RequestMapping(value = "/saveSysMenu",method = RequestMethod.GET)
    @ResponseBody
    public JsonResult saveSysMenuAuth(@RequestParam(required = true) SysGroupVo sysGroupVo ){
//    public JsonResult saveSysMenuAuth(Integer roleId,String groupName,Integer dataManager){
//        SysGroupVo sysGroupVo =new SysGroupVo();
//        sysGroupVo.setRoleId(roleId);
//        sysGroupVo.setGroupName(groupName);
//        sysGroupVo.setDataManager(dataManager);
        roleMenuService.saveSysMenu(sysGroupVo);

        return ok("添加成功");
    }
}
